<?php
/**
 * 用来测试代码
 */
namespace App\Console\Commands;

use App\Events\ActiveDeductionEvent;
use App\Events\CollectionReceiptEvent;
use App\Facades\CommonPlatformRequestFacade;
use App\Facades\MoneyPlatformRequestFacade;
use App\Facades\RepayBuckleRequestFacade;
use App\Http\Controllers\Frontend\V10\Repay\OldRepayController;
use App\Jobs\RepayBuckleJob;
use App\Jobs\RepayCommonJob;
use App\Jobs\RepayCommonJobQueue;
use App\Jobs\RepayJobQueue;
use App\Jobs\RepayMoneyNotifyJobQueue;
use App\Jobs\RepayMoneyNotifyQueue;
use App\Jobs\RepayMonthJobQueue;
use App\Library\ArrayTools\ArrayTools;
use App\Library\Guzzle\CollectionRequest;
use App\Library\Guzzle\CommonPlatformRequest;
use App\Models\RepayRecord;
use App\Models\RepayRecordCommon;
use App\Models\RepayRecordMoney;
use App\Models\RepayRecordMonth;
use App\Models\RepaySummary;
use App\Models\SettingChannel;
use App\Models\SettingRepayRate;
use App\Models\Traits\Repay;
use App\Models\User;
use App\Models\UserBankCard;
use App\Models\UserCertOcr;
use App\Models\UserLoanRequest;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Monolog\Handler\IFTTTHandler;

class TestRepayCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ljf:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
//        $data = [
//            'third_repay_id' => 'RR201903171346451871249785746352',
//            'trade_no' => '3a060cab702c08dc79ba394671615c40',
//        ];
//        $b = MoneyPlatformRequestFacade::getMoneyRepayResult($data);
//        dd($b);

//        $data = [
//            'channelId' => 'JD_WITHHOLD', //支付渠道
//            'orderId' => '16e08da6525197a6392b8872c7d4b964', //用户流水号
//        ];
//        $a = CommonPlatformRequestFacade::getCommonRepayResult($data);
//        dd($a);
//        $a = UserBankCard::where('id',289739)->value('card_num');
//        dd($a);
//        $this->getXiaoZhang();
//        $recordInfo = RepayRecord::find(95996);
//        $event=['summaries'=>$recordInfo->summary_id,'records'=>[$recordInfo->id]];
////        event(new ActiveDeductionEvent($event));
//        event(new CollectionReceiptEvent($recordInfo->id));
//        $record = RepayRecord::find(70899);
//        $data1['loanId'] = 'LO201812021259068109766643520263';
//        $data1['period'] = 3;
//        $result = MoneyPlatformRequestFacade::getRepayPlan($data1);
//        $data = isset($result['data']) ? $result['data'] : '';
//        if(!empty($data)) {
//
//            $is_success = RepayRecordMoney::getMoneyPlanStatus($data['status']);
//            if($is_success) {
//                RepayRecordMoney::setMoneySuccessStatus($record->money, $data);
//
//            }
//        }

        $data = [
        'loanId' => 'LO201903111537594159442461766237',
    ];
        $a = MoneyPlatformRequestFacade::getRepayPlan($data);
        dd($a);


//
//        $data = [
//            'third_repay_id' => 'RR201904110820392531674388522960',
//            'trade_no' => 'ad65a6cacbf65e76ea50f5b63268679b',
//        ];
//        $a = MoneyPlatformRequestFacade::getMoneyRepayResult($data);
//        dd($a);
//        $this->setAllRepayStatus('LO201901071306371679602931533305');

//        $this->setXi();
//        $this->test();
//        $a = RepayRecord::where('repay_date',Carbon::yesterday()->toDateString())->where('status',RepayRecord::STATUS_FOUR)->get();
//        foreach ($a as $k => $v){
////            dd($v);
//            event(new CollectionReceiptEvent($v->id));
//        }
//        $recordInfo = RepayRecord::where('id',125973)->first();
//        $a = RepaySummary::getFines($recordInfo->normal_date,0.003,7500,125973);
//        dd($a);
//        $this->getXiaoZhang();
//         $this->setAllRepayStatus('LO201902121535256049880751718010');
//         $this->getXiaoZhang();
//        $this->getData();
//        $this->setRepayType();


    }

    public function getData(){
        $a = DB::table('test_data')->get();
        $array = array();
        foreach ($a as $k => $v){
            $array[] = [
                'phone' => $v->phone,
                'period'=> $v->period
            ];
        }
        Log::debug(json_encode($array,JSON_UNESCAPED_UNICODE));
}

    public function setRepayType(){
        $data = <<<DA
[{"phone":"18936331783\r"},{"phone":"18876594366\r"},{"phone":"18571574987\r"},{"phone":"15503999156\r"},{"phone":"15894885678\r"},{"phone":"15100665596\r"},{"phone":"15875569007\r"},{"phone":"18294672867\r"},{"phone":"13615493234\r"},{"phone":"13961271035\r"},{"phone":"13566702331\r"},{"phone":"13827217518\r"},{"phone":"13473299536\r"},{"phone":"18057161580\r"},{"phone":"15045421518\r"},{"phone":"15963433763\r"},{"phone":"18508405985\r"},{"phone":"15960445949\r"},{"phone":"18933922506\r"},{"phone":"13850788320\r"},{"phone":"18757631299\r"},{"phone":"13720406662\r"},{"phone":"18949840478\r"},{"phone":"15697349105\r"},{"phone":"13909119952\r"},{"phone":"13759162989\r"},{"phone":"13839788287\r"},{"phone":"13753999911\r"},{"phone":"15287275665\r"},{"phone":"13520653212\r"},{"phone":"13948725224\r"},{"phone":"15868487937\r"},{"phone":"15922657102\r"},{"phone":"15129778692\r"},{"phone":"13107860117\r"},{"phone":"13902283470\r"},{"phone":"13196011371\r"},{"phone":"13802406722\r"},{"phone":"13611365473\r"},{"phone":"13704284961\r"},{"phone":"18620186309\r"},{"phone":"13456647878\r"},{"phone":"15991858630\r"},{"phone":"13536268768\r"},{"phone":"18736055357\r"},{"phone":"15975015852\r"},{"phone":"13972470453\r"},{"phone":"18364479779\r"},{"phone":"18605000970\r"},{"phone":"18196586190\r"},{"phone":"13681963980\r"},{"phone":"13404226810\r"},{"phone":"13591836014\r"},{"phone":"13555800527\r"},{"phone":"15623041396\r"},{"phone":"13988911227\r"},{"phone":"15966567968\r"},{"phone":"\r"},{"phone":"\r"},{"phone":"\r"},{"phone":"\r"},{"phone":"\r"},{"phone":"\r"},{"phone":""}]  

DA;

    }
    public function test(){

    }

    public function setXi(){
        $info = RepayRecord::where('repay_records.status',4)
            ->select(
                'repay_records.id','repay_records.uid',
                'repay_summaries.loan_channel','data_type','loan_request_id','channel',
                'repay_summaries.loan_id'
            )
            ->leftjoin('repay_summaries','repay_summaries.id','repay_records.summary_id')
            ->leftjoin('user_loan_requests','user_loan_requests.id','repay_summaries.loan_request_id')
            ->where('xz_status','<>',4)
            ->where('xz_status','<>',2)
            ->where('repay_records.repay_type',4)
            ->get();
        $oldGd = array();
        $newGd = array();
        $jishang = array();
        $other = array();
        foreach ($info as $k => $v){
            if($v->loan_channel == SettingChannel::CHANNEL_OLD_GUANGDA){
                $oldGd[] = $v;
                continue;
            }
            //新光大
            if($v->data_type == 1){
                if($v->channel == SettingChannel::CHANNEL_GUANGDA){
                    $newGd[] = $v;
                }elseif($v->channel == SettingChannel::CHANNEL_JIXIAO){
                    $jishang[] = $v;
                }

            }elseif($v->data_type == 2){
                if($v->loan_channel == SettingChannel::CHANNEL_OLD_JINSHANG){
                    if($v->channel == SettingChannel::CHANNEL_GUANGDA){
                        $newGd[] = $v;
                    }elseif($v->channel == SettingChannel::CHANNEL_JIXIAO){
                        $jishang[] = $v;
                    }
                }else{
                    $other[] = $v;
                }

            }
        }
        dump(count($oldGd));
        dump(count($jishang));
        dump(count($other));
        dump(count($newGd));
        foreach ($newGd as $k => $v){
            dump($v->loan_id . ' => ' . $v->loan_channel);
        }
    }




    protected function getXiaoZhang(){
        $a = DB::table('repay_xiaozhang')->get();
        $array = array();
        foreach ($a as $k => $v){
            $array[] = [
                'phone' => $v->phone,
                'repay_date' => $v->repay_date,
                'period' => $v->period,
                'money' => $v->money,
                'name' => $v->name
            ];
        }
        Log::debug(json_encode($array,JSON_UNESCAPED_UNICODE));
    }

    /**
     * Description:设置这个loan_id全部结清的方法
     * Author:lijiafei
     * @param $loan_id
     */
    public function setAllRepayStatus($loan_id){
        $summaryInfo = RepaySummary::select(['id'])->where('loan_id',$loan_id)->first();
        if(!empty($summaryInfo)){
            $recordInfo = RepayRecord::where('summary_id',$summaryInfo->id)->first();
            $this->setMoney($recordInfo);
        }
    }

    /**
     * Description:请求资方接口
     * Author:lijiafei
     * @param $recordInfo RepayRecord 表的model
     * @param $tradeNo 1 逐期还款 2 全部还清
     * @return bool
     */
    private function setMoney($recordInfo){
        $questData = $this->getMoneyRequestData($recordInfo);
        if(!$questData){
            return false;
        }
        $moneyData['loanId'] = $recordInfo->loan_id;
        $moneyData['merOrderNo'] = $recordInfo->trade_no;
        $moneyData['currentPeriod'] = $questData['period'];
        $moneyData['totalAmt'] = $questData['totalAmt']; //还款总金额
        $moneyData['principalAmt'] = $questData['amount']; //本金
        $moneyData['interestAmt'] = $questData['lending_money']; //利息
        $moneyData['serviceAmt'] = $questData['repayment_money']; //服务费
        $moneyData['penaltyAmt'] = $questData['fines_money']; //罚息
        $moneyData['operateStatus'] = $questData['operateStatus'];
        $res = MoneyPlatformRequestFacade::setMoneyRepay($moneyData,2);
        dump($res);
        return true;
    }

    /**
     * 封装资方的接口请求数据
     * @param $recordInfo RepayRecord repay_record的model
     * @return  array|boolean data
     */
    protected function getMoneyRequestData($recordInfo){
        //提前结清
        $operateStatus = 'J';
        $period = RepayRecord::where('summary_id',$recordInfo->summary_id)->where('pay_off',RepayRecord::PAY_OFF_ONE)->where('status','<>',RepayRecord::STATUS_FOUR)->orderBy('period','asc')->value('period');
        $data = MoneyPlatformRequestFacade::getRepaymentPlan($recordInfo->loan_id);
        if(empty($data['data'])){
            return false;
        }else{
            $data = $data['data'];
            $total = $data['totalAmount'];
            $amount = $data['principalAmount'];
            $lending_money = $data['interestAmount'];
            $repayment_money = $data['serviceAmount'];
            $fines_money = $data['penaltyAmount'];
        }
        $data = [
            'period' => $period,
            'totalAmt' => $total,//总金额
            'amount' => $amount,//本金
            'lending_money' => $lending_money,//利息
            'repayment_money' => $repayment_money,//提前结清手续费
            'fines_money' => $fines_money,//罚息
            'operateStatus' => $operateStatus,
        ];
        return $data;
    }

}
